PHP Select SQL view no output - php

I creating a php file that pulls a view from an SQL database. Can someone let me know why this isn't working? It seems to be timing out. I am not getting a connection error, either. Thank you in advance.
<?php
require ('mysqli_connect.php');
$sql = "SELECT * FROM testview ;";
$result = mysqli_query($dbc,$sql);
// Check connection if ($dbc->connect_error) {
die("Connection failed: " . $dbc->connect_error); }
$result=mysqli_query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>userID</th><th>first_name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["userID"]."</td><td>".$row["first_name"]."</td></tr>";
}
echo "</table>"; } else {
echo "0 results"; }
}
$dbc->close();
?>
Here is the connection file
<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'root');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'Test');
// Make the connection:
$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' );
?>

give this a go. you were using mysqli and mysql in the same document. this somtime causes issues.
require_once ('mysqli_connect.php');
$q = "SELECT * FROM testview";
$r = mysqli_query($dbc, $q);
//there was no real need to check the connection, you should be doing this in your connection script.
//you where using 'mysqli' above and 'mysql' below.
$row = mysqli_fetch_array($r);
if ($r) {
echo "<table><tr><th>userID</th><th>first_name</th></tr>";
while ($row = mysqli_fetch_array($r)){
echo "<tr><td>" . $row["userID"] . "</td><td>" . $row["first_name"] . " " . $row["last_name"] . "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
close($conn);

You do not have error messages turned on. Therefore when you are getting syntax errors and etc. they are not showing up and you are assuming a time-out. OR perhaps since you have a non-existent function in your die() call maybe it gets confused trying to die but not able to die.
Before you euthanize your code, turn error messages on. Your code will thank you.
Oh, and change
die("Connection failed: " . $dbc->connect_error);
to
die("Connection failed: " . mysql_error($dbc));

Related

How to fix blank MySQL queries after hosting provider upgraded to PHP 7?

Host updated the environment to PHP 7. Had to update db.php and added the following:
<?php
include('administrator/configuration.php');
//echo DATABASE;
//mysql_select_db(DATABASE, mysql_connect(HOSTNAME, DBUSER, DBPASS));
$con = mysqli_connect(HOSTNAME, DBUSER, DBPASS, DATABASE) or die("Error message...");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if (!$con) {
"Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
printf ("Success: A proper connection to MySQL was made! The ???? database is great." . PHP_EOL);
printf("Host information: " . mysqli_get_host_info($con) . PHP_EOL);
echo "<p><hr />";
try {
//$query = sprintf("SELECT database() AS the_db");
$query = sprintf("SELECT COUNT(*) FROM albums");
/* return name of current default database */
if ($result = mysqli_query($con, $query)) {
$row = mysqli_fetch_row($result);
//printf("Default database is %s.\n", $row[0]);
printf($row[0]);
}
//throw new Exception();
} catch (Exception $e) {
print "Something went wrong /n" . $e;
} finally {
// do nothing
}
?>
As per How to change mysql to mysqli?
, I tried making the change mysql_query( -> mysqli_query($con, .
Navigating to ~/administrator/login.php results in a blank page. I edited ~/public_html/administrator/lib/connection.php - with the above change, nothing.
I'm guessing a WordPress upgrade to start then take it from there.
Any other suggestions?

I have trouble connecting to mysql

I've created a database on az.pl I wanted to use old code which works perfectly on other website. I get this error message:
Could not connect to mysql
Here's my code:
$dbh = mysqli_connect("localhost","user","password", "db") or die ("could not connect to mysql");
I've also tried:
$c = mysql_connect("localhost", "user", "password");
mysql_select_db("db");
$result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = mysql_fetch_assoc($result);
echo htmlentities($row['_message']);
Both with the same result, the error message.
I've searched the web but I haven't found exact problem.
Consider following the docs when debugging your connection issue:
http://php.net/manual/en/function.mysqli-connect.php
<?php
$link = mysqli_connect("localhost", "user", "password", "db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
// this will tell you why the connection failed
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
I finally connected to the database. All I needed to do is to change isset to if in the following code:
if (!$_GET['pid']) {
$pageid = '1';
} else {
$pageid = ereg_replace("[^0-9]", "", $_GET['pid']); // filter everything but numbers for security
}
// Query the body section for the proper page
$sqlCommand = "SELECT pagebody FROM pages WHERE id='$pageid' LIMIT 1";
$query = mysqli_query($link, $sqlCommand) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$body = $row["pagebody"];
}
I used MonkeyZeus code to connect to the database.Thank you MonkeyZeus.

Print entire mysql table with php

I know the basics of HTML, and nothing about PHP. I've found a ton of tutorials, but I couldn't get a single one to work. I have a mySQL database working and apache running. How can I display an entire table on my page? I do not know the column names ahead of time.
Edit: Added and adjusted the code. My index page shows this: connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } //$sql = "SHOW TABLES"; $sql = "select * from cpu"; //edit your table name here $res = $mysqli->query($sql); while ($row = $res->fetch_assoc()) { print_r($row); } ?>
This is my entire index.html source:
<?php
$mysqli = new mysqli("192.168.1.2", "webuser", "*****", "OCN");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//$sql = "SHOW TABLES";
$sql = "select * from cpu"; //edit your table name here
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
print_r($row);
}
?>
Once I changed my file to .php, the error message was more clear. It reminded me that I didn't have webuser at 192.168.1.2. I created the user again and gave the correct permissions. The function appears to work, but I'll have more time in a few hours to look at it.
The real problem here is that you see connect_errno) { echo "Failed to... on your page, instead of a line beginning with Failed to... only.
Because your browser thinks that the code from <?php to $mysqli-> is an HTML tag (because of the opening and closing <>, I think your file is a .htm(l) file rather than a .php file.
Just change the file extension and if PHP is installed correctly it should work.
<?php
$mysqli = new mysqli("localhost", "root", "", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//$sql = "SHOW TABLES";
$sql = "select * from tbl_comment"; //edit your table name here
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
print_r($row);
}
?>
From what I understood. You want to display values but you don't know column names.
If that's so, try this.
$mysqli = new mysqli("192.168.1.2", "webuser", "*****", "OCN");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//$sql = "SHOW TABLES";
$sql = "select * from cpu"; //edit your table name here
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
foreach($row as $ind => $val)
{
echo "Column name: $ind, Column Value: $val<br />";
}
echo "<hr />";
}
?>

I am not getting the result properly when conneting to MySQL

For the code below added, i am not getting any result printed.
$con = #mysqli_connect("localhost","root","","temp");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query="SELECT * FROM `login`";
echo $query;
$result=#mysqli_query($query) or die(mysql_error());
while($row=mysqli_fetch_array($result))
{
echo $row["username"];
}
Try the below code it will work
//conection:
$con = mysqli_connect("localhost","root","","temp") or die("Error " . mysqli_error($con));
//consultation:
$query = "SELECT * FROM login" or die("Error in the consult.." . mysqli_error($con));
//execute the query.
$result = $con->query($query);
//display information:
while($row = mysqli_fetch_array($result)) {
echo $row["username"] . "<br>";
}
Use this code as it is.
$con=mysqli_connect("localhost","root","","temp");
$result = mysqli_query($con,"SELECT * FROM login");
while($row = mysqli_fetch_array($result))
{
echo $row["username"];
}
// use this code and plz check your db name
$host='localhost';
$user='root';
$pass='';
$db_name='temp';
$con=mysqli_connect($host,$user,$pass,$db_name);
if($con)
{
echo "db connect succecssfully";
}
$slt="select * from login";
$query=mysqli_query($slt,$con);
while($row=mysqli_fetch_array($query))
{
echo $row["username"];
}
<?php
$con=mysqli_connect("localhost","root","","temp");
// Here localhost is host name, root is username, password is empty and temp is database name.
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
$result = mysqli_query($con,"SELECT * FROM login");
while($row = mysqli_fetch_array($result)) {
echo $row["username"] . "<br>";
}
mysqli_close($con);
?>
Use this. it may solve your problem.
//connection
$con = mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

PHP-mysql_fetch_array return nothing

I've trying to display values from mysql but it return any empty page. The connection is fine but it does not fetch the data from mysql. I tried all the answers from the similar questions asked. But nothing helped. Can somebody please help me? This is the code
$con= mysql_connect($host, $username, $pwd);
if(!$con)
die("not connected". mysql_errno());
echo(Connected);
mysql_select_db("info",$con);
$query="select * from people";
$result= mysql_query($query,$con) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['id']. " - ". $row['people_name'];
echo "<br />";
}
Try to check if your db user,password are correct! I test the code above :
<?php $con=mysqli_connect("localhost","root","","test"); // Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM people");
while($row = mysqli_fetch_array($result)) {
echo $row['id'] . " -- " . $row['people_name']; echo "<br>";
}
?>
and give me the result without error: 10 -- JOHN 11 -- PRADEEP
I just change mysql_connect to mysqli_connect add in $con= mysql_connect($host, $username, $pwd); a dbname. and $con become $con= mysqli_connect($host, $username, $pwd,$dbname); I use mysqli_query instead of mysql_query. Here is a stackQuestion for the mysql vs mysqli in php which can explain you the difference.
Try this
<?php
$con= mysql_connect('hostname', 'username', 'password');
if(!$con)
die("not connected". mysql_errno());
echo("Connected");
mysql_select_db("test",$con);
$query="select * from tabale_name";
$result= mysql_query($query,$con) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['id']. " - ". $row['name'];
echo "<br />";
}
?>
check this
<?php
$con=mysqli_connect("hostname","username","password","info");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM people");
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['people_name'];
echo "<br>";
}
?>
OR
<?php
$con=mysqli_connect("hostname","username","password");
// Check connection
if ($con)
{
echo "connected to db";
}
else
{
echo "not connected to db";
}
$db_selected = mysql_select_db("info", $con);
if (!$db_selected)
{
die ("Can\'t use info: " . mysql_error());
}
$result = mysqli_query("SELECT * FROM people");
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['people_name'];
echo "<br>";
}
?>

Categories