Data is not displaying on php page from phpmyadmin - php

Helo evry1
i want to display data from phpmyadmin table to my php page but the problem is tht it not displaying here is my code
<?php
include('config.php');
$res = mysql_query("SELECT * FROM accord");
while($row = mysql_fetch_assoc($res))
{
$id=$row['id'];
$rupes=$row['rs'];
$mob=$row['mobilen'];
$carmodel=$row['modelcar'];
}
?>
PKR:<?php echo $rupes;?>
here is my code plz help

There might be two problems
1) You have issue with config.php
2) Your query has some issue try putting die like this
mysql_query("SELECT * FROM accord") or die(mysql_error());

You must echo your variables:
<?php
include('config.php');
$res = mysql_query("SELECT * FROM accord");
while($row = mysql_fetch_array($res))
{
echo $id=$row['id'];
echo $rupes=$row['rs'];
echo $mob=$row['mobilen'];
echo $carmodel=$row['modelcar'];
}
?>

Related

PHP echo does not display JSON from server

The connection to the database is successful (it's on a server, not local), the query works fine (I echo the name, street works fine), BUT it never echoes the json string back to me.
I tried several tutorials from Google how to convert mysql to json but I never get a result out of it. No errors either.
<?php
include "dbconnect.php";
$sql = "SELECT * FROM Test ";
$result = mysqli_query($conn, $sql);
$json_array = array();
while($row = mysqli_fetch_assoc($result))
{
$json_array[] = $row;
echo "<br>";
echo $row["name"];
echo $row["street"];
echo $row["farbe"];
}
echo json_encode($json_array);
?>

How to retrieve link from last to first in PHP web service

From below PHP web services code I am able to get the link from first to last which is saved on server database.....
Now I want to get the saved link from last to first.....
<?php
$con=mysqli_connect("XX.XX.XX.X:XXXX","root","root","db_");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM images");
$images = array();
if(mysqli_num_rows($result)) {
while($post = mysqli_fetch_assoc($result)) {
$images[] = $post;
}
}
header('Content-type: application/json');
echo json_encode(array('images'=>$images));
mysqli_close($con);
?>
The output ob above code is:
{"images":[{"image":"http://demo.in/vk/vehicles/v_1.jpg"},{"image":"http://demo.in/vk/vehicles/v_2.jpg"},{"image":"http://demo.in/vk/vehicles/v_3.jpg"},{"image":"http://demo.in/vk/vehicles/v_4.jpg"}]}
but I want It to be come like this:
{"images":[{"image":"http://demo.in/vk/vehicles/v_4.jpg"},{"image":"http://demo.in/vk/vehicles/v_3.jpg"},{"image":"http://demo.in/vk/vehicles/v_2.jpg"},{"image":"http://demo.in/vk/vehicles/v_1.jpg"}]}
Change your query to this
$result = mysqli_query($con,"SELECT * FROM images ORDER BY DESC");

Redirecting user if record isn't in database

I have a URL shortener script that makes the user provide the record ID then redirects them. However, I want to be able to redirect the user to a specific page if the record they searched for does not exist. How can I do this?
My current code is:
<?
mysql_connect("localhost","username","password");
mysql_select_db("database");
$sql="select * from shortened where (shortened.id='".mysql_real_escape_string($_GET['url'])."')";
$query=mysql_query($sql);
while($row = mysql_fetch_array($query)){
header('Location:'.$row['url']);
}
?>
I hope people can understand what I'm trying to describe.
Put
if (!mysql_num_rows($query)) {
header('Location: http://google.com');
exit;
}
right before your while part
And your while could be actually rewritten as:
$row = mysql_fetch_array($query);
header('Location:'.$row['url']);
(yes, just remove while - it is pointless in this case)
<?
mysql_connect("localhost","username","password");
mysql_select_db("database");
$sql="select * from shortened where (shortened.id='".mysql_real_escape_string($_GET['url'])."')";
$query=mysql_query($sql);
$row = mysql_fetch_array($query)
header('Location:'.$row['url']);
?>
while loop is no necessary here
How about this?
<?
mysql_connect("localhost","username","password");
mysql_select_db("database");
$sql="select * from shortened where (shortened.id='".mysql_real_escape_string($_GET['url'])."')";
$query=mysql_query($sql);
if(mysql_num_rows($query) > 0){
$row = mysql_fetch_array($query);
$url = $row['url'];
} else {
$url = "http://othersite.com";
}
header("Location: $url");
?>

For Each loop not echoing data (mysql_fetch_assoc problem?)

Hello and Good Morning,
I am still learning PHP and for some reason my script will not post any data in my foreach loop. Any Idea why? The emailRow Echos out fine but I am going to remove My code is below:
<?php
include 'includes/header.php';
$accountUser = array();
$upgradeEmail = $_GET['currEmail'];
$emailQuery = "SELECT fbID, firstName, lastName FROM users WHERE emailOne='".$upgradeEmail."' AND authLevel=0";
<?php echo $emailRow['fbID']; ?>
<?php echo $emailRow['firstName']; ?>
<?php echo $emailRow['lastName']; ?>
while($emailRow = mysql_fetch_assoc($emailQuery, $conn))
{
$accountUser[]=$emailRow;
}
?>
<table>
<?php foreach($accountUser as $emailData) { ?>
<tr><td> <?php emailData['fbID']; ?> </td><td><?php emailData['firstName']; ?></td><td><?php emailData['lastName']; ?></td></tr>
<?php } ?>
</table>
You have constructed your SQL query in $emailQuery, but never executed it. Call mysql_query(), and pass its result resource to mysql_fetch_assoc().
$emailQuery = "SELECT fbID, firstName, lastName FROM users WHERE emailOne='".$upgradeEmail."' AND authLevel=0";
$result = mysql_query($emailQuery);
if ($result)
{
while($emailRow = mysql_fetch_assoc($result, $conn))
{
$accountUser[]=$emailRow;
}
}
else // your query failed
{
// handle the failure
}
Please also be sure to protect your database from SQL injection by calling mysql_real_escape_string() on $upgradeEmail since you're receiving it from $_GET.
$upgradeEmail = mysql_real_escape_string($_GET['currEmail']);
You don't actually echo anything.
as well as not running the query.
and there are some other methods to do things, much cleaner than usual uglyPHP.
a function
function sqlArr($sql){
$ret = array();
$res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
if ($res) {
while($row = mysql_fetch_array($res)){
$ret[] = $row;
}
}
return $ret;
}
a code
$email = mysql_real_escape_string($_GET['currEmail']);
$data = sqlArr("SELECT * FROM users WHERE emailOne='$email' AND authLevel=0");
include 'template.php';
a template
<? include 'includes/header.php' ?>
<table>
<? foreach($data as $row) { ?>
<tr>
<td><?=$row['fbID']?></td>
<td><?=$row['firstName']?></td>
<td><?=$row['lastName']?></td>
</tr>
<? } ?>
</table>
YOU HAVE A SYNTAX ERROR. You can't open a new php tag within an existing php tag. You have to close the already open tag first.
As far as getting the query to work,
First you have to fetch data before printing it or echoing it...
while($emailRow = mysql_fetch_assoc($emailQuery, $conn))
{
$accountUser[]=$emailRow;
}
then you may write statements..
echo $emailRow['fbID']; etc. code.
Secondly you have not fired a query, just written the query statement. Use mysql_query to fire it.
Your code would be something like this..
<?php include 'includes/header.php';
$accountUser = array();
$upgradeEmail = $_GET['currEmail'];
$emailQuery = mysql_query("SELECT fbID, firstName, lastName FROM users WHERE emailOne='".$upgradeEmail."' AND authLevel=0") or die (mysql_error());
while($emailRow = mysql_fetch_assoc($emailQuery, $conn))
{
$accountUser[]=$emailRow;
}
echo $emailRow['fbID'];
echo $emailRow['firstName'];
echo $emailRow['lastName'];
print '<table>';
foreach($accountUser as $emailData) {
print '<tr><td>'$.emailData['fbID'].'</td><td>'.$emailData['firstName'].'</td><td>'.$emailData['lastName'].'</td></tr>';
}
print '</table';
?>
Feel free to use this code, modifying it to fit your needs.

Help with PHP While function

Why is this not working?
<?php
$select = "select * from messages where user='$u'";
$query = mysqli_query($connect,$select) or die(mysqli_error($connect));
$row = mysqli_num_rows($query);
$result = mysqli_fetch_assoc($query);
$title = mysqli_real_escape_string($connect,trim($result['title']));
$message = mysqli_real_escape_string($connect,trim($result['message']));
while(($result = mysqli_fetch_assoc($query))){
echo $title;
echo '<br/>';
echo '<br/>';
echo $message;
}
?>
where as this works -
<?php
echo $title;
?>
SORRY TO SAY, BUT NONE OF THE ANSWERS WORK. ANY OTHER IDEAS?
If your mysqli query is returning zero rows then you will never see anything printed in your while loop. If $title and $message are not set (because you would want reference them by $result['title'] & $result['message'] if that are the field names in the database) then you will only see two <br /> tags in your pages source code.
If the while loop conditional is not true then the contents of the while loop will never execute.
So if there is nothing to fetch from the query, then you won't see any output.
Does you code display anything, or skip the output entirely?
If it skips entirely, then your query has returned 0 rows.
If it outputs the <br /> s, then you need to check your variables. I could be wrong, not knowing te entire code, but generally in this case you would have something like
echo $result['title'] instead of echo $title
If $title and $message come from your mysql query then you have to access them through the $result array returned by mysqli_fetch_assoc.
echo $result['title'];
echo $result['message'];
Also if your using mysqli you'd be doing something like this:
$mysqli = new mysqli("localhost", "user", "password", "db");
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
print $row['title'];
}
$result->close();
}
Try this:
<?php
$result = mysql_query($query);
while($row = mysqli_fetch_assoc($result)){
echo $title.'<br/><br/>'.$message;
}
?>
Does this work;
<?php
$select = "select * from messages where user='$u'";
$query = mysqli_query($connect,$select) or die(mysqli_error($connect));
$row = mysqli_num_rows($query);
while(($result = mysqli_fetch_assoc($query))){
echo $result['title'];
echo '<br/>';
echo '<br/>';
echo $result['message'];
}
?>
Basically I've made sure that it's not picking the first result from the query & then relying on there being more results to loop through in order to print the same message repeatedly.

Categories