Mysql database

So I decided to make the MySQL database to which I am getting from PHP code in file manager.
When I check if the connection is fine it will write to me, yes it is, but when I want to get data from it I cant. Cant, you write to me if it is a bug or just I can’t have MySQL database or something else
Here is my php code:

<?php $conn = mysql_connect("(mysql host name)","(MySQL User name)", "(mysql password)", "(mysql db name)"); if($conn){ echo "connected"; }else{ echo "didn't connect"; } $sql = "SELECT * FROM `users`;"; $result = mysqli_query($conn, $sql); $resultCheck = mysqli_num_rows($result); if($resultCheck > 0){ while($row = mysqli_fetch_assoc($result)){ echo $row['Username']; } } ?>.

I’m gonna put these into ``` and ``` so everyone can read the codes easily >w<

<?php 
$conn = mysql_connect("(mysql host name)","(MySQL User name)", "(mysql password)", "(mysql db name)");
 if($conn){
 echo "connected";
 }else{
 echo "didn't connect"; 
} 
$sql = "SELECT * FROM `users`;";
$result = mysqli_query($conn, $sql); 
$resultCheck = mysqli_num_rows($result); 
if($resultCheck > 0){ 
while($row = mysqli_fetch_assoc($result)){ 
echo $row['Username'];
}
 } 
?>

Can you use var_dump($row) instead of echo $row['Username']; then post here the results? :slight_smile:

3 Likes

If I do it it will write NULL but I have in my mysql database data.

Then it seems that sql code doesn’t select data. Add var_dump($result), then that should say what’s going on.

I don’t know if I understood it correctly but I added var_dump($result) and it showed NULL.

  1. change mysql_connect to mysqli_connect
  2. change SELECT * FROM 'users'; to SELECT * FROM users
  3. Also enable display error from alter php config in Cpanel. (maybe there’re php warnings that doesn’t show by default)
2 Likes

I did it and it wrote this:

Deprecated : mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/vol7_2/epizy.com/epiz_25259857/htdocs/mysqlcon.php on line 3
connected
Warning : mysql_connect() expects parameter 1 to be string, resource given in /home/vol7_2/epizy.com/epiz_25259857/htdocs/mysqlcon.php on line 11
NULL
Warning : mysql_num_rows() expects parameter 1 to be resource, null given in /home/vol7_2/epizy.com/epiz_25259857/htdocs/mysqlcon.php on line 13

Warning : mysqli_close() expects parameter 1 to be mysqli, resource given in /home/vol7_2/epizy.com/epiz_25259857/htdocs/mysqlcon.php on line 21

I’m sorry to bother you.

The line 21 I removed and 11 and 13 are these lines

$result = mysql_connect($conn, $sql);
$resultCheck = mysql_num_rows($result);

line 3 is $conn = “(My MySQL database db name, username, password and hostname)”

These aren’t the correct codes! I tried to correct them for you:

<?php
$dbhost = "your hostname";
$dbuser = "your username";
$dbpass = "your password";
$dbname = "your database name";

$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
if(!$conn){
 echo "Error during server connection: ".mysqli_connect_error();
 exit;
} 

$sql = "SELECT * FROM `users`";
$result = mysqli_query($conn, $sql); 
$resultCheck = mysqli_num_rows($result); 
if($resultCheck > 0){ 
 while($row = mysqli_fetch_assoc($result)){ 
  echo $row['Username'];
 }
} 
?>

where your hostname can be replaced with your database hostname, your username can be replaced with your hosting account username, your password can be replaced with your hosting account password and your database name can be replaced with your database name. Also, @anon19508339, grave accents can also be used on database systems like MySQL to delimit things like groups, usernames or hostnames on the queries.

4 Likes

Thank you It is borking fine. BIG THANKS

1 Like

IKR? (it just was to make sure there’s not problem in sql)

I was about to see the error message but fortunately I came late :grimacing:

The mysql_* functions were deprecated many years ago and have been removed in PHP 7. You should use the mysqli_* functions instead, or use PDO.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.