Rayj2019 Posted July 23, 2019 Share Posted July 23, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309006-trying-to-read-mysql-database/ Share on other sites More sharing options...
Barand Posted July 23, 2019 Share Posted July 23, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309006-trying-to-read-mysql-database/#findComment-1568624 Share on other sites More sharing options...
Rayj2019 Posted July 23, 2019 Author Share Posted July 23, 2019 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 = ??? Quote Link to comment https://forums.phpfreaks.com/topic/309006-trying-to-read-mysql-database/#findComment-1568630 Share on other sites More sharing options...
Barand Posted July 23, 2019 Share Posted July 23, 2019 (edited) Look more carefully at the examples on that manual page Edited July 23, 2019 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/309006-trying-to-read-mysql-database/#findComment-1568632 Share on other sites More sharing options...
cyberRobot Posted July 23, 2019 Share Posted July 23, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309006-trying-to-read-mysql-database/#findComment-1568635 Share on other sites More sharing options...
MPM Posted July 26, 2019 Share Posted July 26, 2019 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: connect (you do) select the data from the table (you did int he first example) extract a row from that data (as mentioned no need for a loop for the first test just get one row) extract a single item from that array echo that item Or replace 4 and 5 with a print_r of the array. Quote Link to comment https://forums.phpfreaks.com/topic/309006-trying-to-read-mysql-database/#findComment-1568705 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.