0

I have created two functions one for connecting to MySQL database and one for running a specific query. I enter the database name as parameter for first function to connect to the database, this works fine, but my problem is with the second one. 2nd function returns the $result from running a query, but when I use mysql_fetch_array with the $result, it gives one output even if it supposed to give more than one. As I am no php expert so i can't find the solution. Please help me.

Here is the code:

File Function.php

<?php
function myconnect($data)
{
  $db_host='localhost';
  $db_user='root';
  $db_pwd='';
  $data=$data;
  $dbc = mysqli_connect($db_host, $db_user,$db_pwd,$data) or die (mysql_error());
  return $dbc;
}

function runquery($db,$table,$tcol,$tid)//(databse,table,column_name,identifier)
{
  $dbc=myconnect($db);
  $query="SELECT *FROM ".$table." WHERE ".$tcol."=".$tid." ORDER BY first_name ASC";
  $result = mysqli_query($dbc, $query);
  return $result;
}
?>

File test.php

<?php
  require_once('testfunc.php');
  $result= runquery('user','user_basic','user_type','1');
  //runquery('database','table','col','id')/
  while($row=mysqli_fetch_array($result))
  { 
    echo '<strong>First Name:</strong>' . $row['first_name'] . '<br/>';
  }
?>

If I am doing all wrong then suggest me a better way :-)

3
  • your question is not clear enough. Could you elaborate a better description of what you're trying to accomplish? Commented Jan 16, 2012 at 19:11
  • *FROM should be * FROM... problem if $table doesn't have a column called first_name... look to use mysqli rather than mysql; or (even better) pdo Commented Jan 16, 2012 at 19:13
  • This code could use a review. Commented Jan 16, 2012 at 20:26

2 Answers 2

2

A quick glance shows that in your function runquery

SELECT *FROM

should be

SELECT * FROM

note the space after the *

EDIT : I also notice you are using *mysqli_fetch_array* and this is not a valid mysqli method. You are right in using the mysqli extension over mysql but you should look more into statement fetch to solve this issue. The link I provided give a procedural example that should work for what you need.

Sign up to request clarification or add additional context in comments.

Comments

0
function myconnect($db)
{
    /*Removed redundant - single use variables*/

    /*DB name was passed to the client_flags parameter of mysql_connect instead of mysql_select_db*/
    $dbc = mysql_connect("localhost", "root","") or die (mysql_error());
    /*Inserted Line*/
    mysql_select_db($data);
    return $dbc;
}

Currently you're not selecting a database - equivalent of USE DATABASE db_name.

Couple of syntax changes and function definition

function runquery($db,$table,$tcol,$tid)//(databse,table,column_name,identifier)
{
    $dbc=myconnect($db);
    /*Query and link identifier were in the wrong order*/
    return mysql_query("SELECT * FROM ".$table." WHERE ".$tcol."=".$tid." ORDER BY first_name ASC", $doc);
}

Finally a couple of syntax changes, function calls

require_once('testfunc.php');
$result= runquery('user','user_basic','user_type','1');
/*fetch associateive array of result during iteration*/
while($row=mysql_fetch_assoc($result))
{ 
    echo '<strong>First Name:</strong>' . $row['first_name'] . '<br/>';
}

2 Comments

you should not use the mysql extension and instead should use the mysqli extension as it is improved
I haven't read up on it yet. Once I've had a chance see the relevance to the question I'll edit my answer. Thanks for the heads up.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.