Retrieving data from MySQL database - php

Not sure what I'm doing here.
The following code outputs the Database connected statement, but is not displaying any records ("0 results"):
<?php
//Server Details
$host ="localhost";
$user = "X32284679";
$password = "X32284679";
//Connection
$dbc = mysql_pconnect($host,$user,$password);
//Database Selection
$dbname="X32284679";
mysql_select_db($dbname);
if (!$dbc)
{
die("Connection Failed: " .mysqli_connect_error());
}
echo "Connected";
$sql = "select * from staff";
$result = $dbc -> query($sql);
if ($result ->num_rows >0 )
{
echo"<table><tr><th>Email</th><th>Name</th><th>Mobile</th> <th>Address</th><th>Password</th></tr>";
while($row = $result-> fetch_assoc())
{
echo "<tr><td>" .$row["staff_email"]."</td><td>".$row["staff_name"]."</td><td>".$row["staff_mobile"]."</td><td>".$row["staff_address"]."</td><td>".$row["staff_password"]."</td></tr>";
}
echo "</table>";
}
else
{
echo "0 Results";
}
$dbc->close();
?>

this looks like a whole lot of copy & paste from tutorials.
The use of both mysql_ and mysqli_ functions is pretty much useless all together..
As it look slike you want it in mysql_ ive re written your code to fit your needs.
Code:
<?php
//Server Details
$host ="localhost";
$user = "X32284679";
$password = "X32284679";
//Connection
mysql_connect($host,$user,$password) or die("An error occured while connecting...");
//Database Selection
$dbname="X32284679";
mysql_select_db($dbname);
$sql = "select * from staff";
$Query = mysql_query($sql);
if (mysql_num_rows($Query)){
$html .= "<table><tr><th>Email</th><th>Name</th><th>Mobile</th> <th>Address</th><th>Password</th></tr>";
while($row = mysql_fetch_array($Query)){
$html .= "<tr><td>" .$row["staff_email"]."</td><td>".$row["staff_name"]."</td><td>".$row["staff_mobile"]."</td><td>".$row["staff_address"]."</td><td>".$row["staff_password"]."</td></tr>";
}
$html .= "</table>";
}
else{
$html .= "0 Results";
}
echo $html;
mysql_close();
?>

Related

Established connection to database but rendering a blank page

I am simply trying to display data from a table in my database.
For some reason, it keeps rendering a blank page.
I have no training at all. SO I am sure my code looks like crap. But I can usually get it to work. I researched but all I could find were connection problems. I get a 'connected successfully' message but no data and no error message.
<?php
$db_host = 'localhost';
$db_user = 'user';
$db_pass = '';
$db_name = '';
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$query = "SELECT * FROM `my_table`";
$result = mysql_query("SHOW COLUMNS FROM `my_table`");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
mssql_field_name($result,0);
// print_r($row);
}
}
?>
$db_name = ' ' ; is Null write Your Database Name
You should first provide a $db_name, because it's currently blank
$db_pass = '';
you need to provide your database name in here, else your application is literally trying to connect to nothing
You might have to check $mysqli_connect, that the $db_name=''; is null...put your database name inside ''
try like this.
<?php
$conn=mysqli_connect('localhost','root',' ','databasename');
$query="Select * from my_table";
$result = mysqli_query($conn,$query);?>
<table>
<tr>
<th>name</th>
<th>full_name</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["full_name"]; ?></td>
</tr>
<?php
}
?>
Please add your database name and database password, then you can try like this :
$sql = "Select * from table_name";
if ($result=mysqli_query($conn,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
$response[] = $row;
}
} else {
echo 'Could not run query: ' . mysqli_error();
exit;
}
print_r($response);

Trying to echo out every name in database table inside hrml list

I have been trying to echo out database data through a while loop now for awhile but it doesn't work, I can't find where the issue is. I have tried echoing out data manually and that works just fine.
<?php $results = mysqli_query($con,"SELECT * FROM guestbook ORDER BY id DESC"); ?>
<?php while ($row = mysqli_fetch_assoc($results)) : ?>
<li>
<?php echo $row['message']; ?>
</li>
<?php endwhile ?>
First of all make sure you are connected to the database. I don't know if you omitted that on purpose or not. Also, check if the connection is established.
<?php
//Insert your server info here
$servername = "servername";
$username = "root";
$password = "root";
$dbname = "test_database";
// Create and check your connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM guestbook ORDER BY id DESC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo '<li>' . row["message"] . '</li>';
}
} else {
// Your query produced 0 results
echo "Empty result!";
}
// Remember to close the connection
mysqli_close($conn);
?>

I can't get any data output from the database, i only get "0 results" as the else statement, what's the case?

I'm having some troubles with fetching some database information with my php code.
All I'm getting is this message: "Connected successfully0 results".
Here's my code guys, thanks for the help in advance.
<?php
$servername = "example";
$username = "example1";
$password = "example2";
$row = array();
$conn = new mysqli($servername,$username,$password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "Select Distinct subject from mobile_math_science_toc";
$result = mysqli_query($conn, $sql);
if ($result = $conn->query($sql)) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "Subject " ,$row["subject"];
}
} else {
echo "0 results ";
}
mysqli_close($conn);
?>
You should add dbname while creating your connection to database. You can use mysqli_num_rows function to count no. of rows.
<?php
$servername = "example";
$username = "example1";
$password = "example2";
$dbname = "your_db_name"; // Specify your db-name here.
$conn = new mysqli($servername,$username,$password,$dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "Select Distinct subject from mobile_math_science_toc";
$result = mysqli_query($conn, $sql);
// Checking if there are some records available.
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "Subject " ,$row["subject"];
}
} else {
echo "0 results ";
}
mysqli_close($conn);
?>
Change
$conn = new mysqli($servername,$username,$password);
To
$conn = new mysqli($servername,$username,$password, "<your database name>");
And
$result = mysqli_query($conn, $sql);
if ($result = $conn->query($sql)) {
}
To
$result = $conn->query($sql);
if ($result) {
}
Try
if (mysqli_num_rows($result) > 0) {
While checking you got the data or not

mysql_fetch_array($result) echo $rows in html

So I have these specific rows that I'm pulling if a code matches the database but I have no idea on how to echo this to my full html, is there anyway to make this $rows a $_POST or $_get to html?
thanks
<?php
$db_hostname = 'localhost';
$db_database = 'codedb';
$db_username = 'root';
$db_password = '';
$table = 'users';
$field = 'code';
$test = 'first_name';
// Connect to server.
$connection = mysql_connect($db_hostname, $db_username, $db_password) OR DIE ("Unable to
connect to database! Please try again later.");
// Select the database.
mysql_select_db($db_database,$connection)
or die("Unable to select database: " . mysql_error());
$query = "SELECT * FROM $table WHERE $field = '{$_GET["qcode"]}'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_array($result)) {
$name = $row["$field"];
$test = $row["$test"];
echo "Hello: $name $test";
}
} else {
echo "error msg";
}
mysql_close($connection);
?>
You just need to update your while loop
Following code is to create your result Array, by that result array you can use the values in HTML too.
$resArr = array();
while($row = mysql_fetch_array($result)) {
$resArr[] = $row;
}
echo "<pre>";print_R($resArr);exit;
try
$query = "SELECT * FROM '$table' WHERE '$field' = '".$_GET["qcode"]."'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_assoc($result))
{
$name1 = $row[$field];
$test1 = $row[$test];
echo "Hello:" .$name1. $test1;
}
}
else { echo "error msg"; }
Also use mysql_real_escape_string() to prevent sql injection or better to use mysqli or PDO
You have two alternatives :
i) Use a .php file and write the html part there. This way you run php code with simple, php tags and display stuff where you need.
example :
Create a file called test.php and put this code in it and run.
</head>
<body>
<?php
$db_hostname = 'localhost';
$db_database = 'codedb';
$db_username = 'root';
$db_password = '';
$table = 'users';
$field = 'code';
$test = 'first_name';
// Connect to server.
$connection = mysql_connect($db_hostname, $db_username, $db_password) OR DIE ("Unable to
connect to database! Please try again later.");
// Select the database.
mysql_select_db($db_database,$connection)
or die("Unable to select database: " . mysql_error());
$query = "SELECT * FROM $table WHERE $field = '{$_GET["qcode"]}'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result)) {
$name = $row["$field"];
$test = $row["$test"];
echo "<p>".$name." ".$test."</p>";
}
}
else { echo "error msg"; }
mysql_close($connection);
?>
</body>
</html>
This example puts the content in a paragraph in the html.
ii) echo the content from php by encoding it JSON and receive it using jquery from your html form. I'll not elaborate on this since it is not in the scope of the question.
And DO REMEMBER TO USE THE mysql_real_escape_string() to keep your code robust and prevent sql injection.

mysql_query how to display html for if condition

So I have this rustic and basic php that will check a value in my database and if its corrects it will pull out the rest of that row I guess but my problem is how to echo certaine html if the result is okay and how to echo some other html if else.
btw atm else is not working, in other words if you input a code that is not on my db it will not show anything
<?php
$db_hostname = 'localhost';
$db_database = 'codedb';
$db_username = 'root';
$db_password = '';
$table = 'users';
$field = 'code';
$test = 'first_name';
// Connect to server.
$connection = mysql_connect($db_hostname, $db_username, $db_password) OR DIE ("Unable to
connect to database! Please try again later.");
// Select the database.
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$query = "SELECT * FROM $table WHERE $field = '{$_GET["qcode"]}'";
$result = mysql_query($query);
if ($result) {
while($row = mysql_fetch_array($result)) {
$name = $row["$field"];
$test = $row["$test"];
echo "Hello: $name $test";
}
}
else {
echo "Im sorry you buddy, you are not a winner this time! $test";
}
mysql_close($connection);
?>
You can use mysql_num_rows(), it's the best way.
if( mysql_num_rows($result) > 0 ){
/* Anything you want here on success */
} else {
/* Anything you want here on failure */
}
if(mysql_num_rows($result) > 0) { echo "output"; } else { echo "error msg"; }
Use mysql_num_rows against the query.
if(mysql_num_rows($result)){ } else { }
Instead of echoing every html value you can simply enclose html in if statements
like:
<?php
$query = "SELECT * FROM $table WHERE $field = '{$_GET["qcode"]}'";
$result = mysql_query($query);
if ($result) {
while($row = mysql_fetch_array($result)) {
$name = $row["$field"];
$test = $row["$test"];
?>
<b>Hello: <?php echo $name $test"; ?></b>
<?php
}
}
else {
?>
<b> Im sorry you buddy, you are not a winner this time! </b> <?php echo $test";
}
mysql_close($connection);
?>
First you have to correct syntax of database selection like this
mysql_select_db($db_database,$connection);
Insted of
mysql_select_db($db_database);
Check Manual

Categories