Forum Moderators: coopster

Message Too Old, No Replies

Help with MySql Connection and query

         

RobertMallett

1:13 pm on Aug 5, 2020 (gmt 0)

5+ Year Member



Hello,

I'm fundamentally wrong here somewhere but I can't see why (the connect details are stored in a required file but not shown - they work fine in other scripts).

 $connection = new mysqli($server, $dbusername, $dbpassword, $databasename)
or die(mysql_error());

if ($connection->connect_error) {
die("ERROR: Unable to connect: " . $connection->connect_error);
}


$searchStmt = "SELECT * FROM fhs_properties WHERE " ;

if ($search_mots)
{
$searchStmt .= ("desc_fr LIKE '%$search_mots%'");
}
if ($search_mots) $searchStmt .= " ";

//mots 1blank
if (!$search_mots) $searchStmt .=" ";
$or='';
if (!$search_mots)
{
$searchStmt .= ("desc_fr = ' '");
}
if (!$search_mots) $searchStmt .= "OR";

//new
if ($search_new) $searchStmt .=" ";
$or='';
if ($search_new)
{
$searchStmt .= ("new = '$search_new'");
}
if ($search_new) $searchStmt .= " ";


if (!$searchtenant) $searchStmt .= " Order By price ASC ";


$stmt= substr($searchStmt, 0, strlen($searchStmt)-0) ;


echo $searchStmt;


// Connect to the Database

if (!($link=new mysqli($server, $dbusername, $dbpassword))) {

exit() ;
}

// Select the Database
if (!mysqli_select_db($databasename, $link)) {

exit() ;
}

// Execute the Statement

if (!($result = $connection->query($stmt, $link))) {

exit() ;
}

// Display the results of the search

if ($rows = mysqli_num_rows($result))
{
$number = mysqli_num_rows($result);
echo "number of results is $number";
}


The Search Stmt works OK and outputs SELECT * FROM fhs_properties WHERE desc_fr = ' 'OR new = '1' Order By price ASC for instance but I get no results so there must be something wrong on the connect - I should see xx number of records in the result but the result set is empty.

Any help of pointers would be gratefully received.

[edited by: RobertMallett at 1:38 pm (utc) on Aug 5, 2020]

not2easy

1:28 pm on Aug 5, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Hi RobertMallett and Welcome to WebmasterWorld [webmasterworld.com]

Sorry I can't be much help here with PHP but I wanted to welcome you, let you know that more useful responses should be here soon and to let you know that the welcome link above can help you with forum features, how things work and more. We're here to help each other do better.

RobertMallett

1:37 pm on Aug 5, 2020 (gmt 0)

5+ Year Member



Thanks, nice to be back, I used to be a DevShed member a few years back but my login didn't work so i signed up fresh. I'm glad to see the old place hasn't changed too much

not2easy

2:41 pm on Aug 5, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Sorry to tell you but DevShed is gone. Despite extensive efforts to move the old discussions here there were code compatibility issues. Users of that forum are now redirected here and originally we provided a welcome forum for those users but it is not active now. We didn't want the confusion of two Apache Forums for example with duplicate discussions.

Some of the DevShed folks may have migrated elsewhere. Some of the old users (I had an account there) are here and making good use of the WebmasterWorld forums. You can see some of the old posts from last year: [webmasterworld.com...] though there were only two threads started, that forum was created just to welcome DevShed members aboard.

nordberg25

9:27 am on Aug 9, 2020 (gmt 0)

5+ Year Member



First off you are initiating a mysqli connection but your error is only using mysql so if you are having an error in the connection I would start with mysqli_error() after that do a var_dump on $result for starters.

RobertMallett

6:12 am on Aug 10, 2020 (gmt 0)

5+ Year Member



Thanks, yes I figured it in the end, just needed to take a deep breath.