Current location: Hot Scripts Forums » Programming Languages » PHP » Trying to read and write from a table


Trying to read and write from a table

Reply
  #11 (permalink)  
Old 10-27-08, 03:54 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
My guess is you made a spelling mistake in line 23. The error you get says:
Quote:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /opt/lampp/htdocs/Sites/prueba.php on line 23
... but according to my code, you shouldn't even get to this point if the query failed:
PHP Code:

$result mysql_query("select * from clientes") OR die(mysql_error());

$row mysql_fetch_array($result); 
... the second line triggers the error, but the first one would have stopped the script on failure.
Reply With Quote
  #12 (permalink)  
Old 10-27-08, 11:08 AM
tirengarfio tirengarfio is offline
Newbie Coder
 
Join Date: Oct 2008
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry... i forget adding to my code "OR die(mysql_error())" ....


I've just added it, and when i open the page, this message is showed: "No database selected".


My steps are:

- Start the apache mysql php server (XAMPP).
- Open the page with the code we have been discusing.

Should i need something more??

Last edited by tirengarfio; 10-27-08 at 11:16 AM.
Reply With Quote
  #13 (permalink)  
Old 10-27-08, 11:18 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Post your new code. I've added database selection and error handing in mine, but I'm not sure what you've done now...
Reply With Quote
  #14 (permalink)  
Old 10-27-08, 11:41 AM
tirengarfio tirengarfio is offline
Newbie Coder
 
Join Date: Oct 2008
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
My code is below. NOW the error is: "Error conectando a la base de datos".

PHP Code:

<?php

$nombre
="Javier";

$nombre2;


if (!
$link mysql_connect("localhost","",""))
{
    echo 
"Error conectando a la base de datos.";
    exit();
}

if (!
mysql_select_db("ejemplo"))
{
    echo 
"Error conectando a la base de datos.";
    exit();
}

mysql_query("insert into clientes(nombre) values('$nombre')") OR die(mysql_error());

$result mysql_query("select * from clientes") OR die(mysql_error());
$row mysql_fetch_array($result);

echo 
$row["nombre"];

?>
Reply With Quote
  #15 (permalink)  
Old 10-27-08, 12:31 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
What does this output?
PHP Code:

<?php

$nombre 
"Javier";

mysql_connect("localhost""""") OR die(mysql_error());
mysql_select_db("ejemplo") OR die(mysql_error());

mysql_query("insert into clientes(nombre) values('$nombre')") OR die(mysql_error());

$result mysql_query("select * from clientes") OR die(mysql_error());
$row mysql_fetch_array($result);

echo 
$row["nombre"];

?>
Reply With Quote
  #16 (permalink)  
Old 10-27-08, 04:59 PM
tirengarfio tirengarfio is offline
Newbie Coder
 
Join Date: Oct 2008
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Nico View Post
What does this output?
PHP Code:

<?php


$nombre 
"Javier";

mysql_connect("localhost""""") OR die(mysql_error());
mysql_select_db("ejemplo") OR die(mysql_error());

mysql_query("insert into clientes(nombre) values('$nombre')") OR die(mysql_error());

$result mysql_query("select * from clientes") OR die(mysql_error());
$row mysql_fetch_array($result);

echo 
$row["nombre"];

?>
It gives this error:

"Access denied for user ''@'localhost' to database 'ejemplo'"
Reply With Quote
  #17 (permalink)  
Old 10-27-08, 05:23 PM
ianbrind ianbrind is offline
Wannabe Coder
 
Join Date: Jul 2008
Location: Somerset UK!
Posts: 192
Thanks: 0
Thanked 0 Times in 0 Posts
Try using "root" as your database username.
Reply With Quote
  #18 (permalink)  
Old 10-28-08, 02:18 PM
tirengarfio tirengarfio is offline
Newbie Coder
 
Join Date: Oct 2008
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by ianbrind View Post
Try using "root" as your database username.
Hi, as you said i have wroten this:

Code:
mysql_connect("localhost", "root", "") OR die(mysql_error());

And now, when i open the file with my browser i just can see this written: first.

Any idea?!?
Reply With Quote
  #19 (permalink)  
Old 10-29-08, 10:39 AM
tirengarfio tirengarfio is offline
Newbie Coder
 
Join Date: Oct 2008
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Hi again,

I have done a "select * from clientes" and i have seen the first element of my table is "nombre => first", so everything is working OK using "root" as username.

Anyway, why must i use "root" as username?

Last edited by tirengarfio; 10-29-08 at 10:45 AM.
Reply With Quote
  #20 (permalink)  
Old 10-29-08, 10:48 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Because root is usually the default user name. You could change it, but if you're working locally, I wouldn't really bother, to be honest.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you read and write to files dacksal PHP 5 05-15-08 10:12 PM
simple text read and write script Kevin Raleigh Script Requests 1 08-19-06 04:16 PM
Heres one, mysql statement to load a table with a file from ftp?? 0o0o0 PHP 8 04-28-06 01:32 AM
Can javascript be used to read and write to a text file kept on a webserver? gmb1994 JavaScript 4 11-23-04 10:50 AM
Database file or object file is read only, w_inaim ASP 2 09-21-04 02:18 PM


All times are GMT -5. The time now is 08:21 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.