lisa
lisa

Reputation: 13

How to fetch data from database and display it in PHP?

How do I get data from a database using php and show it?

The database table has columns, labeled as ID & Number. ID is unique & fixed whereas Number is just a non-unique number. If someone visits http://example.com/show.php?ID=32, and show.php should fetch the Number & display "Your number is XXX”

Please provide the code-samples.

Upvotes: 0

Views: 27058

Answers (4)

RobertPitt
RobertPitt

Reputation: 57278

<?php
//Connect to DB
$db = mysql_connect("localhst","user","pass") or die("Database Error");
mysql_select_db("db_name",$db);

//Get ID from request
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

//Check id is valid
if($id > 0)
{
    //Query the DB
    $resource = mysql_query("SELECT * FROM table WHERE id = " . $id);
    if($resource === false)
    {
        die("Database Error");
    }

    if(mysql_num_rows($resource) == 0)
    {
        die("No User Exists");
    }

    $user = mysql_fetch_assoc($resource);

    echo "Hello User, your number is" . $user['number'];
}

This is very basic but should see you through.

Upvotes: 1

rajiv
rajiv

Reputation: 1

<?php
$host="localhost"; 
$username=""; 
$password=""; 
$db_name="multiple_del"; 
$tbl_name="test_mysql"; 

// Connect to server and select databse.
mysql_connect("$host", "root", "")or die("cannot connect");
mysql_select_db("multiple_del")or die("cannot select DB");

$sql="SELECT * FROM test_mysql";


$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>

<?php
}
?>

<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

<?php


if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM test_mysql WHERE id='$del_id'";
$result = mysql_query($sql);
}


}

?>

</table>
</form>
</td>
</tr>
</table>



its my code but its not working and error are show in this code:-

<?php


if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM test_mysql WHERE id='$del_id'";
$result = mysql_query($sql);
}


}

?>

please help me.

Upvotes: 0

Deepika
Deepika

Reputation: 474

First get the id of the user(it may given while visiting or based on the details given while visiting)

Then write select query to the table which contains 'number' field.like

SELECT number FROM table1 WHERE table1.ID=IDFromtheuser;

Upvotes: 1

xkeshav
xkeshav

Reputation: 54084

try

SELECT number from numberTable nt
JOIN idTable it ON  it.ID = nt.ID
WHERE it.ID = `your given id`

as i think your both tables are referenced with id

Upvotes: 0

Related Questions