AbbasAkhtar
Programmer
Hi again i need some help with mysql and php again, this is my problem:
Is it possible to retreive only 1 line in a table in a database for example,
if i had a database called mydb and a table within called details, and it's like this
First Name Age
Abbas Akhtar 17
John Smith 23
can i do something to print out the first data in the table details which would be my name and my age
and something to print out the second data in the table which would be john smith whos age is 23,
also, is there any way to count all the datas in a tables, so the count for mine above would be 2 because only 2 datas exists,
do u get wot i mean, i can't explain it any other way.
also, at the top, of my page i use this:
//get values/variables if available
$v = $_POST['agee'];
$v2 = $_POST['namee'];
this works, but if nothing has been posted or the page is loaded for the first time and nothing submitted, it displays on the html page:
Notice: Undefined index: agee in C:\Program Files\Apache Group\Apache2\htdocs\db2.php on line 3
Notice: Undefined index: namee in C:\Program Files\Apache Group\Apache2\htdocs\db2.php on line 4
but if i submit after this once, it wont display it no more, is there any way i can detect if there are any $_POST's available so it wont show the error if nothing has been posted, could someone explain how to fix this,
also, in the php file, i want to know how i can clear the table details in mydb and create it again so i want 2 buttons clear all and create new, this is the code im using but im getting errors ? any ideas
<?php
//get values/variables if available
$v = $_POST['agee'];
$v2 = $_POST['namee'];
// Establish a connection to the MySQL DBMS
$connection = mysql_connect("localhost", "root", "abbas"
// Use the mydb database
mysql_select_db("mydb", $connection);
//if values are greater or less than nothing then do this
if ($v and $v2 <> '')
{
mysql_query ("INSERT INTO details VALUES ('$v2','$v')"
}
// Run a query through the connection
$result = mysql_query("SELECT name, age FROM details", $connection);
// Fetch each row of the results into an array $row
echo "<h2>Database details</h2><p>";
//reconnect to the database to start again from the beginning
mysql_select_db("mydb", $connection);
$result = mysql_query("SELECT name, age FROM details", $connection);
while ($row = mysql_fetch_array($result))
{
echo "First Name: {$row["name"]} Age: {$row["age"]}\n<br>";
}
echo "</select><p>";
//set values/variables to nothing
$result = mysql_query("SELECT name, age FROM details", $connection);
echo "Delete Details:<p><select name=\"select\" size=\"10\">" ;
while ($row = mysql_fetch_array($result))
{
echo "<option>";
echo "{$row["name"]}\n";
echo "</option>";
}
echo "</select><p>";
?>
<form name="clear_all" action="
<?
mysql_connect('localhost','root','abbas');
@mysql_select_db('mydb') or die( 'Unable to select database');
$query = 'DROP TABLE `details` '; ;
mysql_query($query);
mysql_close();
?>
">
<p>
<input name="clear" type="button" id="clear" value="Clear Details">
</form>
<form name="newtable" action="
mysql_connect('localhost','root','abbas');
@mysql_connect_db('mydb');
mysql_query ('CREATE TABLE details (name VARCHAR(255), age VARCHAR(2))');
mysql_close();
">
<input type="button" name="new_db_table" id="new_table" value="Create New Table">
</form>
<?php
$v == '';
$v2 == '';
?>
<p>
<form name="add_details" method="post" action="db2.php">
Enter Name: <input type="textfield" name="namee"> <br>
Enter Age: <input type="textfield" name="agee"> <br>
<input type = "submit" name="submit" value="Add to Database">
</form>
thanks.
Is it possible to retreive only 1 line in a table in a database for example,
if i had a database called mydb and a table within called details, and it's like this
First Name Age
Abbas Akhtar 17
John Smith 23
can i do something to print out the first data in the table details which would be my name and my age
and something to print out the second data in the table which would be john smith whos age is 23,
also, is there any way to count all the datas in a tables, so the count for mine above would be 2 because only 2 datas exists,
do u get wot i mean, i can't explain it any other way.
also, at the top, of my page i use this:
//get values/variables if available
$v = $_POST['agee'];
$v2 = $_POST['namee'];
this works, but if nothing has been posted or the page is loaded for the first time and nothing submitted, it displays on the html page:
Notice: Undefined index: agee in C:\Program Files\Apache Group\Apache2\htdocs\db2.php on line 3
Notice: Undefined index: namee in C:\Program Files\Apache Group\Apache2\htdocs\db2.php on line 4
but if i submit after this once, it wont display it no more, is there any way i can detect if there are any $_POST's available so it wont show the error if nothing has been posted, could someone explain how to fix this,
also, in the php file, i want to know how i can clear the table details in mydb and create it again so i want 2 buttons clear all and create new, this is the code im using but im getting errors ? any ideas
<?php
//get values/variables if available
$v = $_POST['agee'];
$v2 = $_POST['namee'];
// Establish a connection to the MySQL DBMS
$connection = mysql_connect("localhost", "root", "abbas"
// Use the mydb database
mysql_select_db("mydb", $connection);
//if values are greater or less than nothing then do this
if ($v and $v2 <> '')
{
mysql_query ("INSERT INTO details VALUES ('$v2','$v')"
}
// Run a query through the connection
$result = mysql_query("SELECT name, age FROM details", $connection);
// Fetch each row of the results into an array $row
echo "<h2>Database details</h2><p>";
//reconnect to the database to start again from the beginning
mysql_select_db("mydb", $connection);
$result = mysql_query("SELECT name, age FROM details", $connection);
while ($row = mysql_fetch_array($result))
{
echo "First Name: {$row["name"]} Age: {$row["age"]}\n<br>";
}
echo "</select><p>";
//set values/variables to nothing
$result = mysql_query("SELECT name, age FROM details", $connection);
echo "Delete Details:<p><select name=\"select\" size=\"10\">" ;
while ($row = mysql_fetch_array($result))
{
echo "<option>";
echo "{$row["name"]}\n";
echo "</option>";
}
echo "</select><p>";
?>
<form name="clear_all" action="
<?
mysql_connect('localhost','root','abbas');
@mysql_select_db('mydb') or die( 'Unable to select database');
$query = 'DROP TABLE `details` '; ;
mysql_query($query);
mysql_close();
?>
">
<p>
<input name="clear" type="button" id="clear" value="Clear Details">
</form>
<form name="newtable" action="
mysql_connect('localhost','root','abbas');
@mysql_connect_db('mydb');
mysql_query ('CREATE TABLE details (name VARCHAR(255), age VARCHAR(2))');
mysql_close();
">
<input type="button" name="new_db_table" id="new_table" value="Create New Table">
</form>
<?php
$v == '';
$v2 == '';
?>
<p>
<form name="add_details" method="post" action="db2.php">
Enter Name: <input type="textfield" name="namee"> <br>
Enter Age: <input type="textfield" name="agee"> <br>
<input type = "submit" name="submit" value="Add to Database">
</form>
thanks.