Answers.Body: <p>I wouldn't do it that way. I'd remove those first two lines and install <a href="https://github.com/civicrm/cv" rel="nofollow noreferrer">cv</a> - if you are using buildkit you already have it - and then run it with <code>cv scr myscript.php</code>.</p>
  <p>That way it runs on all cmses, and you can do other neat stuff like <code>cv scr --user=demo myscript.php</code> to run it as the user named demo to test different permissions, run it with <code>-vvv</code> to get some debugging, etc...</p>
  <p>Separate note: $contact is not a string so you can't <code>echo</code> it. You can do something like <code>var_export($contact)</code>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-12T17:54:55.757Z
Answers.LastActivityDate: 2022-08-12T17:54:55.757Z
Answers.UserDisplayName: Demerit
Answers.Body: <p>In Combination out of Demerits posts i can solve the problem via</p>
  <p><code>cv scr --user=some_wordpress_username skriptname.php</code></p>
  <p>Thanks for the support.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-19T13:39:59.093Z
Answers.LastActivityDate: 2022-08-19T13:39:59.093Z
Answers.UserDisplayName: gzwg
Body: <p>I tried to write a Standalone PHP-Skript for CiviCRM and looked after the <a href="https://learn.civi.be/wp-admin/admin-ajax.php?action=h5p_embed&amp;id=6" rel="nofollow noreferrer">Simple Example</a>, but unfortunately it doesnt return something.</p>
  <pre><code>&lt;?php
  require_once '/var/www/html/wp-content/plugins/civicrm/civicrm/civicrm.config.php';
  require_once $civicrm_root . '/CRM/Core/Config.php';
  
  
  $config = CRM_Core_Config::singleton();
  
  $contacts = \Civi\Api4\Contact::get()
    -&gt;addSelect('first_name')
    -&gt;addWhere('contact_type', '=', 'Individual')
    -&gt;setLimit(25)
    -&gt;execute();
  
  foreach ($contacts as $contact) {
      echo $contact;
  }
  
  </code></pre>
  <p>The variable $civicrm_root is loaded by the <em>civicrm.config.php</em> and is the absolute path to my civiCRM root: <em>/var/www/html/wp-content/plugins/civicrm/civicrm/</em>.
  I looked up both config files, they exist and I crosschecked the querycode against the APIv4 Explorer, and it works, I dont know what the problem is.</p>
  <p>System:</p>
  <ul>
  <li>Debian 11 VM</li>
  <li>Apache 2.4.54</li>
  <li>PHP 7.4.30</li>
  <li>MariaDB: 10.5.15</li>
  <li>Wordpress: 6.0.1</li>
  <li>CiviCRM: 5.52.0
  I hope there is a Idea for a solution.
  Thanks in advance!</li>
  </ul>
  <p><strong>Edit I</strong></p>
  <p>I applied the suggestions and invoke it with ~/buildkit/bin/cv scr myscript.php but no return value either.</p>
  <pre><code>&lt;?php
  $config = CRM_Core_Config::singleton();
  
  $contacts = \Civi\Api4\Contact::get()
    -&gt;addWhere('contact_type', '=', 'Individual')
    -&gt;setLimit(25)
    -&gt;execute();
  
  foreach ($contacts as $contact) {
      echo $contact['first_name'];
  }
  
  </code></pre>
  <p><strong>EDIT II</strong></p>
  <p>I tried <code>cv api4 Contact.get +w 'contact_type = &quot;Individual&quot;' +l 25</code>
  and it works, still: whats wrong with the script?</p>
  <p><strong>EDIT III</strong></p>
  <p>I insert the line <code> echo count($contacts);</code>, and this return 0.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-08-14T17:34:19.973Z
Comments.Text: By default api4 enforces permissions. When run with `cv scr`, you either need to pass `--user=some_wordpress_username`  on the command line, or instead of `get()` use `get(FALSE)`. When run with `cv api4`, it seems to automatically bypasses permissions - this [appears to be on purpose](https://github.com/civicrm/cv/blob/88ce942e6bc5c08600cbe60b87d4a09973eabe69/src/Command/Api4Command.php#L27).
Comments.UserDisplayName: Demerit
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-08-17T13:33:45.977Z
Comments.Text: Did you see my comment above? You are getting 0 because you are running the script as the anonymous user who doesn't have access to contacts.
Comments.UserDisplayName: Demerit
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-08-19T13:37:06.527Z
Comments.Text: Yes Thanks! It was really helpful! I have to merge my new profile with my unregistred account to post this.
Comments.UserDisplayName: gzwg
Tags: <mysql><php><apiv4>
Title: Php Standalone Skript doesnt Query Database, APIv4 Simple Example
ThreadText: # Php Standalone Skript doesnt Query Database, APIv4 Simple Example
  
  (Asked by: username_0 on 2022-08-12)
  
  I tried to write a Standalone PHP-Skript for CiviCRM and looked after the Simple Example, but unfortunately it doesnt return something.
  addSelect('first_name')
    ->addWhere('contact_type', '=', 'Individual')
    ->setLimit(25)
    ->execute();
  
  foreach ($contacts as $contact) {
      echo $contact;
  }
  
  
  The variable $civicrm_root is loaded by the civicrm.config.php and is the absolute path to my civiCRM root: /var/www/html/wp-content/plugins/civicrm/civicrm/.
  I looked up both config files, they exist and I crosschecked the querycode against the APIv4 Explorer, and it works, I dont know what the problem is.
  System:
  
  Debian 11 VM
  Apache 2.4.54
  PHP 7.4.30
  MariaDB: 10.5.15
  Wordpress: 6.0.1
  CiviCRM: 5.52.0
  I hope there is a Idea for a solution.
  Thanks in advance!
  
  Edit I
  I applied the suggestions and invoke it with ~/buildkit/bin/cv scr myscript.php but no return value either.
  addWhere('contact_type', '=', 'Individual')
    ->setLimit(25)
    ->execute();
  
  foreach ($contacts as $contact) {
      echo $contact['first_name'];
  }
  
  
  EDIT II
  I tried cv api4 Contact.get +w 'contact_type = "Individual"' +l 25
  and it works, still: whats wrong with the script?
  EDIT III
  I insert the line  echo count($contacts);, and this return 0.
  
  **Tags:** mysql, php, apiv4
  
  > **Comments:**
  > **username_1**: By default api4 enforces permissions. When run with `cv scr`, you either need to pass `--user=some_wordpress_username`  on the command line, or instead of `get()` use `get(FALSE)`. When run with `cv api4`, it seems to automatically bypasses permissions - this [appears to be on purpose](https://github.com/civicrm/cv/blob/88ce942e6bc5c08600cbe60b87d4a09973eabe69/src/Command/Api4Command.php#L27).  
  > **username_1**: Did you see my comment above? You are getting 0 because you are running the script as the anonymous user who doesn't have access to contacts.  
  > **username_0**: Yes Thanks! It was really helpful! I have to merge my new profile with my unregistred account to post this.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2022-08-12. Score: 1 | Accepted answer
  
  I wouldn't do it that way. I'd remove those first two lines and install [cv](https://github.com/civicrm/cv) - if you are using buildkit you already have it - and then run it with `cv scr myscript.php`.
  
  That way it runs on all cmses, and you can do other neat stuff like `cv scr --user=demo myscript.php` to run it as the user named demo to test different permissions, run it with `-vvv` to get some debugging, etc...
  
  Separate note: $contact is not a string so you can't `echo` it. You can do something like `var_export($contact)`.
  
  
  ---
  
  ### Answer by username_0 on 2022-08-19. Score: 1
  
  In Combination out of username_1s posts i can solve the problem via
  
  `cv scr --user=some_wordpress_username skriptname.php`
  
  Thanks for the support.
  
  
  ---
  
  