Results 1 to 9 of 9
-
23rd Nov 2011, 01:44 PM #1OPToo busy :|Website's:
L337Fx.com BeastieBay.netDisplaying result from MySQL ? (Help !)
I'm currently learning how to insert data in MySQL database and fetching data from MySQL database.
I have learned how to insert, but displaying the result from MySQL is giving me some problems.
This is the command to create the tables. I'm confused with the id structure and I guess thats the main root of the error I'm getting at the output.
PHP Code:$query="CREATE TABLE post (id int NOT NULL auto_increment,title text(300) NOT NULL,cover text NOT NULL,info text NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
Now the php code to fetch the data from the mysql db is this
PHP Code:$db = mysql_connect($hostname,$username,$password);
if(!$db){
echo 'Failed to connect to database. Check your info';
}
@mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT * FROM post";
/*$result = mysql_query($query);*/
$result = mysql_query($query) or die(mysql_error());
mysql_close();
$id = $_GET['id'];
$title=mysql_result($result,$id,"title");
echo 'ID='.$id.' - Title='.$title.'';
PHP Code:Warning: mysql_result() [function.mysql-result]: Unable to jump to row 5 on MySQL result index 5 in C:\xampp\htdocs\post\preview.php on line 87
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 5 on MySQL result index 5 in C:\xampp\htdocs\post\preview.php on line 88
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 5 on MySQL result index 5 in C:\xampp\htdocs\post\preview.php on line 89
ID=5Title=
Code:ID Title Delete 2 This Is Title Delete Preview 3 This Is Title Delete Preview 4 This Is Title Delete Preview 5 This Is Title Delete Preview 6 This Is Title Delete Preview
BlaZe Reviewed by BlaZe on . Displaying result from MySQL ? (Help !) I'm currently learning how to insert data in MySQL database and fetching data from MySQL database. I have learned how to insert, but displaying the result from MySQL is giving me some problems. This is the command to create the tables. I'm confused with the id structure and I guess thats the main root of the error I'm getting at the output. $query="CREATE TABLE post (id int NOT NULL auto_increment,title text(300) NOT NULL,cover text NOT NULL,info text NOT NULL,PRIMARY KEY (id),UNIQUE id Rating: 5
-
23rd Nov 2011, 03:33 PM #2OPToo busy :|Website's:
L337Fx.com BeastieBay.netI modified the code and currently, this is working fine. I know its a lame and poor code, but its still doing what it should do.
PHP Code:<?php $id = $_GET['id'];
$db = mysql_connect($hostname,$username,$password);
if(!$db){
echo 'Failed to connect to database. Check your info';
}
@mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT * FROM post";
/*$result = mysql_query($query);*/
$result = mysql_query($query) or die(mysql_error());
$title = mysql_result($result,--$id,"title");
$cover = mysql_result($result,$id,"cover");
$info = mysql_result($result,$id,"info");
echo 'ID='.++$id.' - Title='.$title.'';
mysql_close();?>
-
23rd Nov 2011, 03:49 PM #3MemberWebsite's:
wscripts.net damnlolscript.com lulzjet.comPHP Code:<?php
$id = $_GET['id'];
$db = mysql_connect($hostname,$username,$password);
if(!$db){
echo 'Failed to connect to database. Check your info';
}
@mysql_select_db($database) or die( "Unable to select database");
if(isset($id)&&is_int($id)) $where = " where id='".mysql_real_escape_string($id)."'";
$query = mysql_query("SELECT * FROM post".$where) or die(mysql_error());
$row = mysql_fetch_assoc($query);
$title = $row['title'];
$cover = $row['cover'];
$info = $row['info'];
echo 'ID='.$id.' - Title='.$title.'';
mysql_close();
?>
-
23rd Nov 2011, 03:54 PM #4OPToo busy :|Website's:
L337Fx.com BeastieBay.netOh nice t3od0r
you even secured it
But unfortunatelyI'm getting an error
Code:Notice: Undefined variable: where in C:\xampp\htdocs\post\user.php on line 23
-
23rd Nov 2011, 03:55 PM #5MemberWebsite's:
sborg.usPHP Code:<?php
$db = mysql_connect($hostname,$username,$password);
if(!$db){
echo 'Failed to connect to database. Check your info';
}
@mysql_select_db($database) or die( "Unable to select database");
$id = $_GET['id'];
$query = "SELECT * FROM post WHERE id = $id";
$result = mysql_query($query) or die(mysql_error());
mysql_close();
$data = mysql_fetch_array($result);
/* Debug statements
* echo "<pre>";
* print_r($data);
* echo "</pre>";
*/
echo "<br />ID: " . $data['id'];
echo "<br />Title: " . $data['title'];
echo "<br />Cover: " . $data['cover'];
echo "<br />Info: " . $data['info'];
?>
Make sure you add proper conditional statements before making the query, t3o did that
V3g3ta | Halcyon | Abhi
-
23rd Nov 2011, 03:58 PM #6OPToo busy :|Website's:
L337Fx.com BeastieBay.net@Halcyon,
It worked !
Thanks a million !and thanks to t3od0r too
I'm gonna now use his technique to secure the code hehe
====
EDIT:
I have a question, how did the $id take the value inside a string ?
I mean $query = "SELECT..... id = $id" how did the $id variable take the value ? Its inside a string and normally it'd be like $query = "SELECT ... id=".$id;
? right ? ^ I did that though, but it gave me errors. Still didn't knew that the $var can take values inside a string
-
23rd Nov 2011, 04:09 PM #7Member
Either should work.
PHP Code:echo $id;
echo "id: $id";
echo "id: ".$id;
-
23rd Nov 2011, 04:12 PM #8MemberWebsite's:
sborg.usIt's PHP buddy!
PHP Code:<?php
$id = 10;
echo "ID: $id"; //shows ID: 10
echo "ID: ".$id; //shows ID: 10
echo 'ID: '.$id; //shows ID: 10
echo 'ID: $id'; //shows ID: $id
?>
V3g3ta | Halcyon | Abhi
-
23rd Nov 2011, 04:25 PM #9OPToo busy :|Website's:
L337Fx.com BeastieBay.neto.o really didn't knew that lol
I always thought that the $var converts to a normal text inside " " or ' '
lol
Thanks guysThanks for everything
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Apache displaying massive Reading request
By khan007 in forum Server ManagementReplies: 7Last Post: 11th Aug 2012, 03:28 PM -
Need help with Wordpress links displaying problem
By glavier in forum Technical Help Desk SupportReplies: 3Last Post: 16th Apr 2012, 07:47 PM -
Displaying Random Post Widget
By {Psycho} in forum Web Application/Script SupportReplies: 2Last Post: 11th Feb 2011, 11:32 AM -
Why are avatars not displaying???
By gamelord in forum General DiscussionReplies: 4Last Post: 23rd Jul 2010, 06:27 AM -
(Tut) Displaying katzporn button in specific forum
By raj11 in forum vBulletinReplies: 6Last Post: 7th May 2010, 01:59 PM
themaLeecher - leech and manage...
Version 5.03 released. Open older version (or...