-5

I have the following code to check if a row exists in MySQL:

<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT 1 FROM files WHERE id='$code' LIMIT 1");
if (mysql_fetch_row($result)) {
    echo 'Exists';
} else {
    echo 'Does not exist';
}
}
?>

This works fine. But I need to change it a bit. I have the following fields: id, title, url, type. When someone uses the code above ^ to check if a row exists, I need a variable to get the url from the same row, so I can redirect the user to there.

Do you have any idea how I can do that?

Thanks in advance! :)

Phantom
  • 1,704
  • 4
  • 17
  • 32
  • If you want to keep on going being unsafe, [check this](http://nl3.php.net/mysql_fetch_row). Otherwise, otherwise, [read this](http://stackoverflow.com/a/60496/777850). And please use google or show some effort on some other way next time. – giorgio Jul 25 '14 at 08:07
  • @giorgio Thanks. So if I understand correctly, I can do $row[row_number]? I don't get what variable I should use instead of $row though.. – user18393299102190328 Jul 25 '14 at 08:28
  • OK, so I did `$row = mysql_fetch_row($result);` and then `echo 'Ja' . $row[0];`, but every lookup now returns 'Does not exist'.. and I don't see `$row[0]`. – user18393299102190328 Jul 25 '14 at 08:31

3 Answers3

0

It is quite simple. I think you don't show any effort to find the solution by yourself.

<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT url FROM files WHERE id='$code' LIMIT 1");
if ($result) {
  $url = mysql_fetch_row($resultado);
} else {
   echo 'Does not exist';
}
}
Rumpelstinsk
  • 3,107
  • 3
  • 30
  • 57
0
<?php
$sql_query = "SELECT * FROM test WHERE userid ='$userid'";
$result1 =mysql_query($sql_query);
if(mysql_num_rows($result1)>0){
while($post = mysql_fetch_array($result1))
{
$url = $post['url'];    

    }
    }
?>

If mysql_num_rows($result1)>0 it means row is existed fir the given user id

Jha
  • 23
  • 10
0

Try this:

<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT * FROM files WHERE id=" . $code . " LIMIT 1");
if (mysql_num_rows($result) > 0) {
    while($rows = mysql_fetch_array($result)) {
         echo 'Exists';
         $url = $rows['url'];
    }
 } else {
    echo 'Does not exist';
 }
}
?>
Lemuel Botha
  • 659
  • 8
  • 22
  • `Notice: Undefined index: url in ... on line 50` Sorry, I'm a beginner. – user18393299102190328 Jul 25 '14 at 08:41
  • Sorry, there was a mistake, try the new code, I think I fixed the issue – Lemuel Botha Jul 25 '14 at 09:03
  • Nope... when it exists same error as above, when it doesn't no output or error. – user18393299102190328 Jul 25 '14 at 09:16
  • Sorry, I didn't see that you did a fetch row instead of a fetch array, so it makes sense why you would receive that error as my declared rows variable was a single row and not an array, it should work now -- Sorry about the error, the pc I am currently on does not have a php server installed locally. – Lemuel Botha Jul 25 '14 at 09:20
  • Also, just FYI, mysql_* functions have been deprecated, you shouldn't use them anymore, rather use PDO and/or MySQLi, it is safer and more secure – Lemuel Botha Jul 25 '14 at 09:22
  • OK, I understand :) I'm still getting an error though. When it doesn't exist I just get Does not exist, when it does I get Exists `Notice: Undefined index: url in /home/wdele/domains/hypah.org/public_html/code.php on line 50`.. And thanks for the advice. – user18393299102190328 Jul 25 '14 at 09:27
  • Are you 100% sure your column name is url? That should be the only thin wrong in the above code t=is the URL column name – Lemuel Botha Jul 25 '14 at 09:32
  • Thanks for your help, but I don't think you understand what I am trying to do. Let's say: the user looks up value `1` via the form. PHP checks if `1` exists in the row `2`. If it does, it outputs value `3` from the form. – user18393299102190328 Jul 25 '14 at 09:38
  • So the URL is not coming from a database, it is coming from a form? – Lemuel Botha Jul 25 '14 at 09:43
  • It's coming from a database: on the `create.php` a user can fill in a form with the code (ID) and URL, which is sent to the database and stored there. On the `code.php` the user should be able to enter a code and PHP should automatically look up what code is assigned to it. That's what I'm asking for :) – user18393299102190328 Jul 25 '14 at 10:09
  • Ok, I noticed a few errors in your select statement which is why it did not work, I have fixed them (see the code above) you can alter your select statement to return only the url aswell, but returning all will allow you to get other rows if you ever need – Lemuel Botha Jul 25 '14 at 11:05