Jump to content

Trying to read mysql database


Rayj2019

Recommended Posts

So I have a very small database right now.   It looks like this:

mysql> select * from broadcast;
+----+-------------------+
| id | streamID          |
+----+-------------------+
|  1 | lxdlp0u20D1QdLt6s |
+----+-------------------+
1 row in set (0.00 sec)

I can update the streamID just fine using another php file.

The problem I am having it accessing it and reading it out.

Here is the php/mysql I am using.  When I browse to this I never see the output!

<?php
$debug = TRUE;
ini_set( "log_errors", TRUE);
ini_set( "error_log", "/var/log/php_error.log");
ini_set( "display_errors", $debug );
error_reporting(E_ALL);

  //--------------------------------------------------------------------------
  // Example php script for fetching data from mysql database
  //--------------------------------------------------------------------------
  $servername = "localhost";
  $username = "root";
  $password = "1*General2019";
  $dbname = "WebRTCApp";
  $tableName = "broadcast";

  //--------------------------------------------------------------------------
  // 1) Connect to mysql database
  //--------------------------------------------------------------------------
  //
  mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  $conn = mysqli_connect($servername,$username,$password, $dbname);
  if (mysqli_connect_error()) {
          $err = mysqli_connect_error();
          if( $debug ) exit( "Error: $err" );
          logerr( $err );
          exit( "Failed to connect to the database!");
  }

  echo "Connected Successfully";

  $sql = "SELECT streamID FROM broadcast WHERE id=1";
 // $sql = 'SELECT id FROM broadcast WHERE streamID = E0ArubgkL9q2SCrXL';
  $stream = mysqli_query($conn,$sql);

        if( !$stream) {
                $err = mysqli_error( $conn );
                logerr( $err );
                if( $debug ) echo "Error: $err<br>" };

  echo "<br>";
  echo "stream = " $stream;

  mysqli_close($conn);

?>

Any idea why I am not seeing the stream (mysql streamID)?

Appreciate your response.

Ray

Link to comment
Share on other sites

2 hours ago, Barand said:

You have to fetch the row from your result set that the query returned

https://www.php.net/manual/en/mysqli-result.fetch-assoc.php

Does not seem to be working?

 $sql = "SELECT streamID FROM broadcast WHERE id=1";
  if ($result = mysqli_query($conn, $sql)) {
          $row = $result->fetch_assoc();

 echo "stream = " $row; }

 

stream = 

???

Link to comment
Share on other sites

Note that fetch_assoc() returns an associative array, if there are matching results. So you'll need to modify how you are using $row.

Also note that fetch_assoc() returns NULL when there are no (or no more) rows in the result set. So you'll want to test the value of $row before assuming it contains a "streamID".

As Barand mentioned, the manual provides examples for using fetch_assoc(). Assuming that your query will always return 1 row, you don't need to use a loop to process the output from fetch_assoc(). You could use a plain if statement.

Link to comment
Share on other sites

You can't just paste the example code in as they are using different variable names to you.  You have to learn the principles and then apply them.  The steps are:

  1. connect (you do)
  2. select the data from the table (you did int he first example)
  3. extract a row from that data (as mentioned no need for a loop for the first test just get one row)
  4. extract a single item from that array
  5. echo that item

Or replace 4 and 5 with a print_r of the array.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.