Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

How to integrate SQL query with PHP


  • Please log in to reply

#1
viki00762

viki00762

    New Member

  • Member
  • Pip
  • 3 posts

Hey guys help an intern out

I need to connect to a mySQL DB through PHP and print that data on the browser

Please help me with PHP integration with SQL.

Thanks in advance.


  • 0

Advertisements


#2
Spike

Spike

    nOoB

  • Member
  • PipPipPipPip
  • 1,357 posts

Good day Viki and welcome to Geeks to Go,

 

Hey guys help an intern out

I need to connect to a mySQL DB through PHP and print that data on the browser

Please help me with PHP integration with SQL.

Thanks in advance.

I am certain there are several ways of achieving exactly what you would like to do all differing in their implementations and capabilities. You could go about doing it from scratch, using a library or even an entire framework.

 

Seeing as you mentioned you're an intern I can assume you would like an easy to understand, easy to implement solution; and I would therefor recommend approaching this by using a library, namely MeekroDB. There might be simpler libraries out there, but I have personally used Meekro and would vouch for its simplicity and ease of use. Again I cannot compare this library to any others and tell you why it's the best, but I can tell you it's as simple as including the library, setting up your connection details and writing the query which puts the results in an array (There's examples on the front page of the site).

 

I hope this somehow solves your problem, if not please don't hesitate to respond back with a more detailed description of exactly what it is your require from connecting to your MySQL database.

 

MeekroDB: https://www.meekro.com

 

Regards,

Spike :cool:


  • 1

#3
viki00762

viki00762

    New Member

  • Topic Starter
  • Member
  • Pip
  • 3 posts

Hey Spike,

 

Thanks a lot for the recommendations,  right now i am testing my logic on mysqli and as soon as it works i will switch to a different library.

I am able to successfully run my php but i am unable to figure out how to send the input data from user to the query.
For eg. this function.
function GetVisitors($website){

$data = $website
$sql = "SELECT * FROM visitors where website = :data and last_visit<= NOW() - Interval 7 Day order by last_visit LIMIT 15";
}
I am getting this error: Notice: Trying to get property of non-object in test.php for :website

 

Please help me send the input passed to function to the sql query.

Thanks.


Edited by viki00762, 02 June 2017 - 08:31 AM.

  • 0

#4
Spike

Spike

    nOoB

  • Member
  • PipPipPipPip
  • 1,357 posts

Hey there again Viki,

 

If you're set on using mysqli, it'll definitly help you appreciate 3rd party libraries at a later stage but/and still recommend you use something like Meekro.

 

Although to assist you with your current delema; I am not certain if the code you have posted is final, but I see a few syntax errors.

 

So lets first deal with the error php is throwing out which is the "property of non-object..." which is a syntax error. You cannot pass parameters when building a string like you're doing with ":data" param, instead you require using the string concat operator which is the period character (.). It's possible you've seen the :param pass in a framework example or something. Some libraries allow you to pass these parameters directly in a function (Eg. query("select * from where field = %s", $param). But for your example the appropriate way is below:

 

Another "possible" error that could occur in your query depends on the expected type of the $data var... I don't want to make any assumptions about its expected type so have listed both options below depending on what it should be. So to clarify, if the $data var is of type integer or float then use the following query:

$sql = "SELECT * FROM visitors where website = " . $data . " and last_visit<= NOW() - Interval 7 Day order by last_visit LIMIT 15";

On the other hand if the $data var is a string type then remember to encompass your param in single quotes (') like in the query below:

$sql = "SELECT * FROM visitors where website = '" . $data . "' and last_visit<= NOW() - Interval 7 Day order by last_visit LIMIT 15";

This also assume that your query actually runs, I mean by a brief glance you query seems fine, although I always recommend you try run the query stand alone in sql and see if you get results or if you have a sql syntax error. But if you get results then you know you should recieve those same results in php. For example if your $data var is actually a string type and you passed it as an integer then you'd get an sql syntax error.

 

I did pickup another syntax error which is where you set "$data = $website". It appears you have forgotten to put a semi-colon (;) at the end of the line. Remember to put semi-colons.

$data = $website;

Last comment... Again I am uncertain if you posted the final code or if this was just an example for me to get the idea... But I see you have a function which you pass some parameter to (GetVisitors), then you create a $sql var but never do anything with it. In other words you never actually call mysqli to run the query. My assumption is that this is obviously example code.

 

Please let me know if you need assistance with anything else.

 

Peace Out,

Spike :cool:


  • 1

#5
viki00762

viki00762

    New Member

  • Topic Starter
  • Member
  • Pip
  • 3 posts

Hey Spike,

 

Thanks a lot for the notes.
I am done with that task using PDO .
Right now i am struggling with the mail function of php
I am using XAMP server and I have already changed my sendmail.ini and php.ini but on running the below script:
 
<?php
 
$subject= 'Hi There';
 
 
$body='This is a demo email sent using PHP on XAMPP';
 
if (mail($to,$subject,$body, $from)){
 
 echo 'Mail sent successfully!';
 
}
 
else{
 
 echo 'Mail not sent!';
 
}
 
?>
 

Although, I am getting 'Mail sent successfully!' whereas I am not receiving the mail.

Kindly help me with this.

Thanks


  • 0

#6
Spike

Spike

    nOoB

  • Member
  • PipPipPipPip
  • 1,357 posts

Good day again Viki,

 

I am sooo sorry I am only replying now! I'm certain by now, you've probably already resolved your problem... 

 

Just in-case you haven't; your snippet seems fine from first view, although I see the last parameter of your mail() function, you are passing only the "from" email address.... The last parameter is the "header" parameter containing all header information including "from" address... It's usually in the form of:

$header = "From: [email protected]";

You can pass multiple header information such as a carbon copy addresses or even the content type... See w3schools link on mail function for details on usage.

 

Also not sure about your entire config, but make sure that your sendmail is setup with the appropriate details and works, if that still doesn't work, check your spam box, it could get caught up in there. Another option if you're still struggling with all this is possibly using a 3rd party mail library which allows you to simply provide your email credentials and call a simple function to email.

 

Sorry again for the late reply... Let me know if you came right or if you're still struggling.

 

Peace Out :cool:


  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP