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 & fix
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;
<?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.
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
<?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"> </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.